package logic

import (
	"context"
	"fmt"
	"hotkid/model/mysql"
	"hotkid/pkg"

	"hotkid/hotkidrpc/hotkidrpc"
	"hotkid/hotkidrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *hotkidrpc.RegisterRequest) (*hotkidrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var users mysql.User
	mysql.DB.Debug().Find(&users)
	if users.ID != 0 {
		return nil, fmt.Errorf("查询失败")
	}
	createUser := mysql.User{
		Account:  in.Account,
		Password: pkg.Md5(in.Password),
	}
	mysql.DB.Debug().Create(&createUser)

	return &hotkidrpc.RegisterResponse{
		Id: int64(createUser.ID),
	}, nil
}
